package log;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;



public class Log {

	private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
	//private static String path = "U:/myworkspace/IstarJade_20072010/file/log/";
	//private static String path = "U:\\LattesScholar\\log\\";

	//protected static String path = "D:\\Documents and Settings\\csid\\workspace\\Fanxaria\\file\\log\\";
	
	//private static String path = "C:/workspace/IstarJade_Home/file/log/";

	private static final String executionTime = dateFormat.format(new Date());
	private BufferedWriter out = null;
	private HashMap<String, File> logFiles = new HashMap<String, File>(); 
	private String pathToLogFile ="";
	// The singleton instance of this LOG
	private static Log theInstance;
	
	// This is the method to access the singleton log object
	public static Log getInstance(String _pathToLogFile){
		
		theInstance = new Log(_pathToLogFile);
		return theInstance;
	}
	
	// Private constructor
	protected Log(String _pathToLogFile){
		super();
		pathToLogFile = _pathToLogFile;
	}
	
	public void log(String agentName, String _phrase)  {
		DateFormat currentTimeFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
		File output=null;
		String file_name = pathToLogFile + agentName + "-log-" + executionTime + ".log";
		output = logFiles.get(file_name);
		try {
			if (output==null){
				output = new File(file_name);
				output.createNewFile();
				logFiles.put(file_name, output);
			}

			FileWriter fw = new FileWriter(output, true);
			BufferedWriter out = new BufferedWriter(fw);
			out.write(currentTimeFormat.format(new Date())+ ": "+_phrase);
			out.newLine();
			out.flush();
			out.close();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	

}
